package main.sevices;

import java.io.Serializable;
import java.util.Iterator;
import java.util.List;

import javax.annotation.ManagedBean;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.enterprise.context.SessionScoped;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import main.entities.Patient;
import main.enums.JsonResponseStatus;
import main.interfaces.PatientBusinessLocal;
import main.models.JsonResponseModel;

@ManagedBean("PatientServices")
@SessionScoped
@Path("/patientservice")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class PatientServices implements Serializable {
    
    /**
	 * GUID for Serializable.
	 */
	private static final long serialVersionUID = 7524309437651465923L;

	private List<Patient> patients = null;
    
    @EJB(name = "PatientBusiness")
    private PatientBusinessLocal _patientEjb;
    
    public PatientServices() {
        
    }
    
    @PostConstruct
    public void init() {
    	patients = _patientEjb.getAllPatients();
    }
    
    @GET
    @Path("/patients")
    public JsonResponseModel getPatients() {
    	patients = _patientEjb.getAllPatients();
    	return new JsonResponseModel(JsonResponseStatus.SUCCESS, "", patients);
    }
    
    @POST
    @Path("/patient/save")
    public JsonResponseModel savePatient(Patient patient) {
        int index = -1;
        
        try {
        	patient = _patientEjb.savePatient(patient);
        }
        catch(Exception e) {
        	return new JsonResponseModel(JsonResponseStatus.EXCEPTION, e.getMessage(), null);
        }
        
        for(int i = 0; i < patients.size(); i++) {
        	if(patients.get(i).getId().equals(patient.getId())) {
        		index = i;
        		break;
        	}
        }
        
        if(index > -1) {
        	patients.set(index, patient);
        }
        else {
        	patients.add(patient);
        }
        
        System.out.println("Patient [" + patient.getfName() + " " + patient.getlName() + "] was saved.");
        return new JsonResponseModel(JsonResponseStatus.SUCCESS, "", null);
    }
    
    @PUT
    @Path("/patient/add")
    public JsonResponseModel addPatient(Patient patient) {
        try {
        	_patientEjb.addPatient(patient);
        	patient = _patientEjb.getPatient(patient.getId());
        }
        catch(Exception e) {
        	return new JsonResponseModel(JsonResponseStatus.EXCEPTION, e.getMessage(), null);
        }
        
        patients.add(patient);
        
        System.out.println("New patient [" + patient.getfName() + " " + patient.getlName() + "] created.");
        return new JsonResponseModel(JsonResponseStatus.SUCCESS, "", patient);
    }
    
    @DELETE
    @Path("/patient/delete/{id}")
    public JsonResponseModel deletePatient(@PathParam("id") Long id) {
        Patient foundPatient = null;
        boolean deleteSuccess = false;
        Iterator<Patient> patientIter = patients.iterator();
        
        while(patientIter.hasNext()) {
            foundPatient = patientIter.next();
            if(foundPatient.getId() == id.intValue()) {
                patientIter.remove();
                break;
            }
            else {
                foundPatient = null;
            }
        }
        
        if(foundPatient != null) {
        	try {
        		deleteSuccess = _patientEjb.deletePatient(foundPatient);
        	}
        	catch(Exception e) {
        		return new JsonResponseModel(JsonResponseStatus.EXCEPTION, e.getMessage(), null);
        	}
        	
        	if(deleteSuccess) {
	            System.out.println("Patient [" + foundPatient.getfName() + " " + foundPatient.getlName() + "] was deleted.");
	            return new JsonResponseModel(JsonResponseStatus.SUCCESS, "", null);
        	}
        	else {
        		System.out.println("An exception occurred while trying to delete [" + foundPatient.getfName() + " " + foundPatient.getlName() + "].");
	            return new JsonResponseModel(JsonResponseStatus.EXCEPTION, "An exception occurred while trying to delete [" + foundPatient.getfName() + " " + foundPatient.getlName() + "]. Please contact the System Administrator.", null);
        	}
        }
        else {
            System.out.println("Patient not found for id [" + id + "].");
            return new JsonResponseModel(JsonResponseStatus.INVALID, "No Patient could be found for ID [" + id + "].", null);
        }
    }
    
}
